package com.cincc.cinccimdemo.ui.conversation.detail;

import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.MyUtils;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.widget.SimpleTextWatcher;
import com.cincc.im.core.IMSipPhoneCtrl;


public class EditGroupInfoActivity extends IMBaseActivity {
    private static final String TAG = "EditGroupInfoActivity";

    @Override
    protected int contentLayout() {
        return R.layout.activity_edit_group_info;
    }

    TextView groupInfoText;
    TextView nameEditText;
    private MenuItem confirmMenuItem;
    int type = 0;
    String groupId = "";

    @Override
    protected void bindViews() {
        super.bindViews();
        groupInfoText = findViewById(R.id.groupInfoText);
        nameEditText = findViewById(R.id.nameEditText);
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        String data = getIntent().getStringExtra("data");
        groupId = getIntent().getStringExtra("groupId");
        //type 0:群聊名称，1：群公告，2：群备注，3：群昵称
        type = getIntent().getIntExtra("type", 0);

        if (type == 1) {
            groupInfoText.setText("群公告");
        } else if (type == 2) {
            groupInfoText.setText("群备注");
        } else if (type == 3) {
            groupInfoText.setText("群昵称");
        } else {
            groupInfoText.setText("群聊名称");
        }
        nameEditText.setText(data);
    }

    @Override
    protected int menu() {
        return R.menu.set_group_info;
    }

    @Override
    protected void afterMenus(Menu menu) {
        confirmMenuItem = menu.findItem(R.id.confirm);
        confirmMenuItem.setEnabled(!nameEditText.getText().toString().trim().isEmpty());
        nameEditText.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                confirmMenuItem.setEnabled(!nameEditText.getText().toString().trim().isEmpty());
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.confirm) {
            updateGroupInfo();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void updateGroupInfo() {
        String data = nameEditText.getText().toString().trim();
        if (type == 0) {
            IMSipPhoneCtrl.getInstance().updateGroupName(groupId, data, imResponse -> {
                Log.e(TAG, "updateGroupName callback: " + imResponse.getCode());
                String result = imResponse.getCode() == 0 ? "修改成功" : imResponse.getMsg();
                runOnUiThread(() -> finishUI(result));
            });
        } else if (type == 1) {
            IMSipPhoneCtrl.getInstance().updateGroupNotice(groupId, data, imResponse -> {
                Log.e(TAG, "updateGroupNotice callback: " + imResponse.getCode());
                String result = imResponse.getCode() == 0 ? "修改成功" : imResponse.getMsg();
                runOnUiThread(() -> finishUI(result));
            });
        } else if (type == 2) {
            IMSipPhoneCtrl.getInstance().updateGroupRemark(groupId, data, imResponse -> {
                Log.e(TAG, "updateGroupRemark callback: " + imResponse.getCode());
                String result = imResponse.getCode() == 0 ? "修改成功" : imResponse.getMsg();
                runOnUiThread(() -> finishUI(result));
            });
        } else if (type == 3) {
            IMSipPhoneCtrl.getInstance().updateNameInGroup(groupId, data, imResponse -> {
                Log.e(TAG, "updateNameInGroup callback: " + imResponse.getCode());
                String result = imResponse.getCode() == 0 ? "修改成功" : imResponse.getMsg();
                runOnUiThread(() -> finishUI(result));
            });
        }
    }

    private void finishUI(String data) {
        Toast.makeText(EditGroupInfoActivity.this, data, Toast.LENGTH_SHORT).show();
        finish();
    }
}
